/**
  * vuex store
  */
import Vue from 'vue'
import Vuex from 'vuex'
import api from '../service/api.js'
import axios from 'axios'

/**
 * modules
 */
import modules from './modules'

Vue.use(Vuex)

export const MUTATION_TYPE_LONGIN = 'login'

console.log(modules)

export default new Vuex.Store({
  modules,
  state: {
    appName: '登陆门户',
    loginUser: null
  },
  getters: {},
  mutations: {
    [MUTATION_TYPE_LONGIN]: (state, view) => {
      api.login({
        payload: view.payload,
        callback: function (response) {
          if (response && response.status === 200) {
            const {code, msg, data} = response.data
            console.log(response)
            if (code === '0') {
              const {user, token} = data
              console.log(token)
              axios.defaults.headers.common['Authorization'] = token
              state.loginUser = user
              state.token = token
              view.$router.push({name: 'Main'})
            } else {
              alert(msg)
            }
          }
        }
      })
    }
  },
  actions: {}
})
